<?php

/**
 * Exception.php
 *
 * @package Plun_Exception
 * @author QuangLT
 * @since May 30, 2012
 * @version 1.0
 */
class Plun_Exception extends Zend_Exception
{

    /**
     * Construct the exception
     *
     * @param  string $msg
     * @param  int $code
     * @param  Exception $previous
     * @return void
     */
    public function __construct($msg = '', $code = 0, Exception $previous = null)
    {
        parent::__construct($msg, (int) $code, $previous);

        $this->_log($msg = '', $code = 0, $previous);
    }

    protected function _log($msg = '', $code = 0, Exception $previous = null)
    {                       
        $stream = @fopen(realpath(APPLICATION_PATH . '/../logs/exception') . '/' . date('d-m-Y') . '.log' , 'a', false);
        
        if (!$stream)
        {
            throw new Exception('Failed to open stream');
        }
        
        $logger = new Zend_Log(new Zend_Log_Writer_Stream($stream));
        
        
        $logger->err(implode(PHP_EOL, array('- Time: ' . date('d/m/Y H:i:s'), '- Code: ' . $this->getCode(), '- Message: ' . $this->getMessage(), '- File: ' . $this->getFile(), '- Line: ' . $this->getLine(), '- Stack Trace: ' . $this->getTraceAsString())) . str_repeat(PHP_EOL, 2));
    }

}